﻿using System;
using System.IO;
using System.Reactive.Disposables;

namespace CliWrap.Tests.Utils.Extensions;

internal static class EnvironmentExtensions
{
    extension(Environment)
    {
        public static IDisposable SetTempEnvironmentVariable(string name, string? value)
        {
            var lastValue = Environment.GetEnvironmentVariable(name);
            Environment.SetEnvironmentVariable(name, value);

            return Disposable.Create(() => Environment.SetEnvironmentVariable(name, lastValue));
        }

        public static IDisposable ExtendPath(string path) =>
            SetTempEnvironmentVariable(
                "PATH",
                Environment.GetEnvironmentVariable("PATH") + Path.PathSeparator + path
            );
    }
}
